<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Model {
  
  var $table_name = 'products';

  function addProduct($deal_id, $name, $cost, $discount, $price_before) {
        
    $data = array('deal_id'=>$deal_id, 'name'=>$name, 'cost'=>$cost, 'discount'=>$discount,
      'price_before'=>$price_before
    );
    $this->db->insert($this->table_name, $data);
  }

  function editProduct($id, $deal_id, $name, $cost, $discount, $price_before) {
        
    $data = array(
      'prod_id'=>$id, 'deal_id'=>$deal_id, 'name'=>$name, 'cost'=>$cost, 'discount'=>$discount,
      'price_before'=>$price_before
    );
    $this->db->where('prod_id',$id);
    $this->db->set($data);
		$this->db->update($this->table_name);
  }

  function deleteProduct($id){
    $this->db->delete($this->table_name, array('prod_id' => $id));
  }

  function getProductsByDealId($id){
    $this->db->where('deal_id', $id);
    $query = $this->db->get($this->table_name);
    return $query->result();
  }

  function getProductById($id){
    $this->db->where('prod_id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }


}